package com.sse.product.controller.rbac;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sse.product.common.ResultCode;
import com.sse.product.rbac.entity.User;
import com.sse.product.webservice.rbac.request.QueryUserByNameLikeRequest;
import com.sse.product.model.rbac.ResponseVo;
import com.sse.product.webservice.rbac.request.AddUserRoleRequest;
import com.sse.product.rbac.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author junyongli
 * @since 2022-07-22
 */
@RestController
@RequestMapping("/rbac/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "根据用户名模糊查询用户")
    @PreAuthorize("hasAuthority('USER-MANAGE')")
    @PostMapping("/queryUserByNameLike")
    public ResponseVo queryUserByNameLike(@RequestBody QueryUserByNameLikeRequest request) {
        IPage result = userService.queryUserByKeyWord(request.getUserNameLike(),request.getPageNo(), request.getPageSize());
        return ResponseVo.assemble(ResultCode.SUCCESS, result);
    }

    @ApiOperation(value = "新增用户")
    @PreAuthorize("hasAuthority('USER-MANAGE')")
    @PostMapping("/add")
    public ResponseVo addRole(@RequestBody User user) {
        return ResponseVo.assemble(ResultCode.SUCCESS, userService.addUser(user));
    }

    @ApiOperation(value = "更新用户")
    @PreAuthorize("hasAuthority('USER-MANAGE')")
    @PostMapping("/update")
    public ResponseVo updateRole(@RequestBody User user) {
        return ResponseVo.assemble(ResultCode.SUCCESS, userService.updateUser(user));
    }

    @ApiOperation(value = "删除用户")
    @PreAuthorize("hasAuthority('USER-MANAGE')")
    @PostMapping("/delete")
    public ResponseVo delete(@RequestBody User user) {
        return ResponseVo.assemble(ResultCode.SUCCESS, userService.deleteUser(user));
    }

    @ApiOperation(value = "根据用户(id)查询角色")
    @PreAuthorize("hasAuthority('USER-MANAGE')")
    @PostMapping("/queryRole")
    public ResponseVo queryRoleByUser(@RequestBody User user) {
        return ResponseVo.assemble(ResultCode.SUCCESS, userService.queryRoleByUser(user));
    }

    @ApiOperation(value = "绑定用户(id)和角色(id)之间的关系【包括新增更新】")
    @PreAuthorize("hasAuthority('USER-MANAGE')")
    @PostMapping("/addUserRole")
    public ResponseVo addUserRole(@RequestBody AddUserRoleRequest request) {
        return ResponseVo.assemble(ResultCode.SUCCESS, userService.addUserRole(request.getUser(), request.getRoleList()));
    }
}
